// 2024.12.05, jinfeng
//
// TODO: use (be, bs), not (bs, be) to match spec definition

#ifndef _ZSTACK_BITOPS_H_
#define _ZSTACK_BITOPS_H_

typedef unsigned int U32;
typedef unsigned long long U64;

#define BIT(n) (1 << (n))

#define BITMASK(bs, be) ((U32)(((U64)1 << ((be) - (bs) + 1)) - 1) << (bs))

#define BITCLR(value, bs, be) ((value) = (value) & (~BITMASK(bs, be)))

#define BITGET(value, bs, be) (((value) & BITMASK(bs, be)) >> (bs))

#define BITSET(value, bs, be, bv)           \
    do                                      \
    {                                       \
        BITCLR(value, bs, be);              \
        (value) = (value) | ((bv) << (bs)); \
    } while (0)

#endif